<template>
	<div class="container">
            <div class="handle-box">
				<el-dropdown :hide-on-click="false">
                    <el-button class="green mr10" plain>操作</el-button>
					<template #dropdown>
						<el-dropdown-menu>
                            <el-dropdown-item type="text" @click="func_batchHandlePass" class="green "> 通过</el-dropdown-item>
                            <el-dropdown-item type="text" @click="func_batchHandleReject" class="red "> 拒绝</el-dropdown-item>
						</el-dropdown-menu>
					</template>
				</el-dropdown>
            </div>
		<el-table :data="vCommonentTableData" border class="table" header-cell-class-name="table-header" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column prop="id" label="ID" width="55" align="center" v-if="false"></el-table-column>
			<el-table-column prop="commonInfo" label="评论内容"></el-table-column>
			<el-table-column prop="isShow" label="评论是否显示" >
                    <template #default="scope">
						{{scope.row. isShow}} - {{scope.row.isShow === 1? '显示': '不显示'}}
				    </template>
            </el-table-column>
			<el-table-column prop="commonter" label="评论人"></el-table-column>
			<el-table-column prop="createTime" label="发布时间"></el-table-column>
			<el-table-column label="操作" :width="optColumnWidth" align="center" >
				<template #default="scope">
					<el-button type="text" icon="el-icon-edit" @click="func_handlePass(scope.$index, scope.row)">通过
					</el-button>
					<el-button type="text" icon="el-icon-delete" class="red" @click="func_handleReject(scope.$index, scope.row)">拒绝</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div class="pagination">
			<el-pagination background layout="total, prev, pager, next" :current-page="query.pageNum"
				:page-size="query.pageSize" :total="dataTotal" @current-change="func_handlerPageChange"></el-pagination>
		</div>
        <el-footer>
            <el-button @click="func_closeDialog" plain>取 消</el-button>
        </el-footer>
	</div>
</template>

<script>
import { onMounted, ref, reactive, toRefs, watch } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { ElMessage, ElMessageBox } from "element-plus";
import { findCommontInfoListPage, batchEditIsShow } from "../../../../api/hairmanager/commonent";

import { NO_DATA_SELECT_ERROR, SUCCESS_DELDATA_DIALOG_MESSAGE, SUCCESS_EDITDATA_DIALOG_MESSAGE } from "../../../../api/CommonErrorMessage.js";

export default {
    name: "newsCommonentPlug",
	props:{
		selectValue: Object
	},
	emits: ["commonentData"],
    setup(props, cex) {
        const router = useRouter();
		const route = useRoute();
        const multipleSelection = ref([])
		onMounted(()=>{
		
		});
        const query = reactive({
			dataId: '',
            crFrom: 0,
			pageNum: 1,
            pageSize: 10,
        });
        const vCommonentTableData = ref([]);
        const dataTotal = ref(0);
        const optColumnWidth = 150;
        // =========================================================================================================
        // 主业务处理 ========================================================================================= START
        // 获取表格数据
        const func_getCommonentData = () => {
            findCommontInfoListPage(query).then((res) => {
                vCommonentTableData.value = res.data.list;
                dataTotal.value = res.data.total;
            });
        };
        // 查询操作
        const handleSearch = () => {
            query.pageNum = 1;
            func_getCommonentData();
        };
        // 分页导航
        const func_handlerPageChange = (val) => {
            query.pageNum = val;
            func_getCommonentData();
        };
        const func_batchHandlePass = (index, row) => {
            if(multipleSelection.value.length ===0){
                ElMessage.warning(NO_DATA_SELECT_ERROR);
                return false;
            }
            let obj = {ids: multipleSelection.value, isShow: 1};
            editIsShow(obj);
            func_closeDialog();
        };
        const func_batchHandleReject = (index, row) => {
            if(multipleSelection.value.length ===0){
                ElMessage.warning(NO_DATA_SELECT_ERROR);
                return false;
            }
            let obj = {ids: multipleSelection.value, isShow: 0};
            editIsShow(obj);
            func_closeDialog();
        };
        const func_handlePass = (index, row) => {
            let obj = {ids: [row.id], isShow: 1};
            editIsShow(obj);
        };
        const func_handleReject = (index, row) => {
            let obj = {ids: [row.id], isShow: 0};
            editIsShow(obj);
        };
        const editIsShow = (v) => {
            batchEditIsShow(v).then((res) => {
                if(res.code != 200) {
                    if(res.type == 0) {
                        ElMessage.warning(res.message);
                    } else {
                        ElMessage.error(res.message);
                    }
                } else {
                    ElMessage.success(SUCCESS_EDITDATA_DIALOG_MESSAGE);
                    func_getCommonentData();
                }
            });
        }
        // 主业务处理 ========================================================================================= END
        const handleSelectionChange = (val) => {
            let ids = [];
            val.forEach((element) => {
                ids.push(element['id']);
            });
            multipleSelection.value = ids;
        };
		const initParams = () => {
			dataTotal.value = 0;
			func_getCommonentData();
		};
        const func_closeDialog = () => {
            multipleSelection.value = [];
            cex.emit('commonentData',  {state: 1, load: true });
        }
        // =========================================================================================
        watch(() => props.selectValue, (val) => {
            query['dataId'] = val['id'];
            query['crFrom'] = 101;
            func_getCommonentData();
            multipleSelection.value = [];
        }, {deep: true,immediate: true});
        // =========================================================================================
        return {
			// 系统变量
            optColumnWidth, query, vCommonentTableData, dataTotal,
			// 主调用方法
            handleSearch, func_handlerPageChange, func_getCommonentData,  func_closeDialog, 
            handleSelectionChange, func_handlePass, func_handleReject, func_batchHandlePass, func_batchHandleReject, editIsShow,
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
